<?php
/*
 * This class holds the request data and processes this data to create
 * a response object.
 */
class request
{
    // Properties
    private $method     = null;
    private $app        = null;
    private $controller = null;
    private $action     = null;
    private $params     = array();

    public function __construct($app=null,$controller=null,
        $action=null,$params=array(),$method = 'GET')
    {
        $this->method     = $method;
        $this->app        = $app;
        $this->controller = $controller;
        $this->action     = $action;
        $this->params     = $params;
    }

    public function process()
    {
        // Try to load app
        if (!is_null($this->app) && file_exists(APPS.$this->app))
        {
            define('THISAPP',$this->app);
            define('APP_PATH',APPS.$this->app.DS);
            define('CONTROLLERS',APP_PATH.'controllers'.DS);
            define('APPLIBS',APP_PATH.'libs'.DS);
            define('PUBLIC',APP_PATH.'public_html'.DS);
            define('VIEWS',APP_PATH.'views'.DS);

            // Controller
            if (!is_null($this->controller) && 
                file_exists(CONTROLLERS.$this->controller.'.php'))
            {
                require_once(CONTROLLERS.$this->controller.'.php');

                // action
                $response = new response;
                $controller_name = 'controller_'.$this->controller;
                $controller_obj = new $controller_name($response);
                if (!is_null($this->action) && 
                    method_exists($controller_obj,$this->action))
                {
                    // Default view
                    $response->view($this->controller.'_'.$this->action);
                    call_user_func_array(array(&$controller_obj,$this->action),$this->params);
                    return $response;
                }
                else
                {
                    return new response('notfound');
                }
            }
            else
            {
                return new response('notfound');
            }
        }        
        else
        {
            return new response('notfound');
        }
    }

    public function __get($name)
    {
        return $this->$name;
    }
}
?>
